/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/* 
 * Copyright (c) 2009 City University of Hong Kong
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License m_version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author: Wilson Thong (wilsonwk@ee.cityu.edu.hk) 
 */
#ifndef PACKET_TRANSIST_HOP_CALCULATOR_H
#define PACKET_TRANSIST_HOP_CALCULATOR_H

#include "ns3/socket.h"
#include "ns3/traced-callback.h"
#include "ns3/application.h"
#include "ns3/ipv4-header.h"
#include <set>

namespace ns3 {
namespace eecityu {

using namespace std;

class DefaultTtlTag : public Tag 
{
public:
  DefaultTtlTag ();
  void SetTtl (uint8_t ttl);
  uint8_t GetTtl (void) const;

  static TypeId GetTypeId (void);  
  virtual TypeId GetInstanceTypeId (void) const;
  virtual uint32_t GetSerializedSize (void) const;
  virtual void Serialize (TagBuffer i) const;
  virtual void Deserialize (TagBuffer i);
  virtual void Print (std::ostream &os) const;

private:
  uint8_t m_ttl;
};

/**
 * \brief Calculate the number of hops traversed by packets
 *
 * The class measures the number of hops traversed by a packet
 * by looking into the difference of the ttl value bewteen the packet
 * just leaves a node and after the packet just arrives at a node.
 *
 * To obtain the tansist hop count, subscribe a callback to the
 * trace source "TransistHopRx".
 *
 * To use this class, subscribe its PrepareTx () to the "Tx"
 * trace source and subscribe its PrepareRx () to the "Rx"
 * trace source.
 */
class PacketTraversedHopCalculator : public Object
{
public:
  PacketTraversedHopCalculator (void);
  static TypeId GetTypeId (void);

  /**
   * \brief Prepare a packet for calculating traversed hops
   *
   * \param packet The packet being monitored for the calculation
   *
   * The PrepareTx () registers the packet to this object so as to
   * ensure the correction assumption on the TTL values
   */
  void PrepareTx (Ptr<const Packet> packet);
  /**
   * \brief Record the traversed hops of the packet received
   *
   * \param packet The arrival packet 
   * \param ipHeader The IPv4 header
   *
   * The RecordRx () reads into the packet's TTL value and 
   * calculating the value different from what it was before it is sent
   * into the network. The default TTL is set by this object and
   * only the packets registered with this object can be processed
   * successfully by this method
   */
  void RecordRx (Ptr<Packet> packet, Ipv4Header const& ipHeader);

protected:
  virtual void DoDispose (void);

private:
  /**
   * \brief Used by the attribute sysem
   *
   * \return The source application from where the packets's TTL value are monitored
   * for calculating traversed hops
   */
  Ptr<Application> GetSourceApplication () const;
  /**
   * \brief Used by the attribute sysem
   *
   * \param app The source application from where the packets's TTL value are monitored
   * for calculating traversed hops
   */
  void SetSourceApplication (Ptr<Application> app);

  /// Trace source for accessing transist hops of arrived packets
  TracedCallback<uint32_t> m_getTraversedHopTrace;
  /// Packets prepared by this object are registered here and only the registered packets are processed at receiving ends
  set<uint32_t> m_registeredPacket;
  /// A reference to the source application for setting the default TTL value
  Ptr<Application> m_sourceApplication;
  /// The default value for all packets being sent out of the source application associated with this object
  const static uint8_t g_defaultTtl = 64;
};

} // namespace eecityu
} // namespace ns3


#endif //#ifndef PACKET_TRANSIST_HOP_CALCULATOR_H
